
package view;
import java.awt.Frame;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

/**
 * Vista del editor de tableros
 * @author Samuel Chávez, Axel Mayorga
 */
public class EditChessboardView extends JPanel
{
   private JTextField txt_height;
   private JTextField txt_queens;
   private JTextField txt_width;
   private JButton btn_Ok;
   private boolean ok;
   private JDialog dialog;
   
   public EditChessboardView()
   {
      setLayout(new BorderLayout());

      JPanel panel = new JPanel();
      panel.setLayout(new GridLayout(3, 2));
      panel.add(new JLabel("Height:"));
      panel.add(txt_height = new JTextField(""));
      panel.add(new JLabel("Width:"));
      panel.add(txt_width = new JTextField(""));
      panel.add(new JLabel("Queens:"));
      panel.add(txt_queens = new JTextField(""));
      add(panel, BorderLayout.CENTER);
      
      btn_Ok = new JButton("Ok");
      btn_Ok.addActionListener(new ActionListener()
         {
            @Override
            public void actionPerformed(ActionEvent event)
            {
               ok = true;
               dialog.setVisible(false);
            }
         });

      JButton cancelButton = new JButton("Cancel");
      cancelButton.addActionListener(new ActionListener()
         {
            @Override
            public void actionPerformed(ActionEvent event)
            {
               dialog.setVisible(false);
            }
         });


      JPanel buttonPanel = new JPanel();
      buttonPanel.add(btn_Ok);
      buttonPanel.add(cancelButton);
      add(buttonPanel, BorderLayout.SOUTH);
     
   }


   public boolean showDialog(Component parent, String title)
   {
      ok = false;

      Frame owner = null;
      if (parent instanceof Frame) owner = (Frame) parent;
      else owner = (Frame) SwingUtilities.getAncestorOfClass(Frame.class, parent);


      if (dialog == null || dialog.getOwner() != owner)
      {
         dialog = new JDialog(owner, true);
         dialog.add(this);
         dialog.getRootPane().setDefaultButton(btn_Ok);
         dialog.pack();
      }

      dialog.setTitle(title);
      dialog.setVisible(true);
     
      return ok;
   }
   
    public void addOkButtonListener(ActionListener  al)
    {
        if(btn_Ok.getActionListeners()!=null && btn_Ok.getActionListeners().length>0)
            btn_Ok.removeActionListener(btn_Ok.getActionListeners()[0]); 
        
        btn_Ok.addActionListener(al);
    }
    
    public int getChessboardHeight()
    {
        try{
            return Integer.parseInt(txt_height.getText());
        }catch(Exception ex){
            return 0;
        }
    }
    
    public void setChessboardHeight(int height)
    {
        this.txt_height.setText(String.valueOf(height));
    }
    
    public int getChessboardWidth()
    {
        try{
            return Integer.parseInt(txt_width.getText());
        }catch(Exception ex){
            return 0;
        }
    }
    
    public void setChessboardWidth(int width)
    {
        this.txt_width.setText(String.valueOf(width));
    }
    
    public int getChessboardQueens()
    {        
        try{
            return Integer.parseInt(txt_queens.getText());
        }catch(Exception ex){
            return 0;
        }
    }
    
    public void setChessboardQueens(int queens)
    {
        this.txt_queens.setText(String.valueOf(queens));
    }
    
    public void setChessboardVisible(boolean visible)
    {
        ok = !visible;
        dialog.setVisible(visible);
    }
    
    public void showErrorMessageHeight(String msg)
    {
        this.txt_height.setToolTipText(msg);
        this.txt_height.setBackground(new Color(255, 170, 170));
    }
    
    public void showErrorMessageWidth(String msg)
    {
        this.txt_width.setToolTipText(msg);
        this.txt_width.setBackground(new Color(255, 170, 170));
    }
    
    public void showErrorMessageQueens(String msg)
    {
        this.txt_queens.setToolTipText(msg);
        this.txt_queens.setBackground(new Color(255, 170, 170));
    }
}